package com.qf.dao;

import com.qf.pojo.User;

import com.qf.utils.ApacheUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/20 22:16
 */
public class UserDaoImpl implements UserDao{
    private QueryRunner qr = new QueryRunner(ApacheUtil.getDataSource());
    @Override
    public User findByUserName(String username) {
        String sql = "select * from tb_user where username=? ";
        try {
            return qr.query(sql,new BeanHandler<User>(User.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into tb_user (username, password, email, gender, status, role, code) value(?, ?, ?, ?, ?, ?, ?)";
        int count = 0;
        try {
            count = qr.update(sql, user.getUsername(), user.getPassword(), user.getEmail(),
                    user.getGender(), user.getStatus(), user.getRole(), user.getCode());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
